
import { Component, OnInit, ViewChild } from '@angular/core';
import 'rxjs/add/operator/map';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
import 'rxjs/add/operator/toPromise';
declare const $: any;
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';

@Component({
    selector: 'app-config-wp',
    templateUrl: './config-wp.component.html',
    styleUrls: ['./config-wp.component.css']
})

export class ConfigWPComponent implements OnInit {
    title = 'Working Pace配置';

    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public shiftItems: any[];
    public equipemntItems: TVResource[] = [];
    public subAareSelected: any;
    public shiftSelected: any;
    public equipmentSelected: TVResource[];

    public items: any[];
    public timeItems: any[];

    public subAreaItems1: SubArea[];
    public subAareSelected1: any;
    public timeRange = '';
    sxTarget = 0;
    xxTarget = 0;

    @ViewChild('dddb') dddb: DxDropDownBoxComponent;

    constructor(private commServices: CommService) {
    }

    ngOnInit() {
        this.shiftItems = this.commServices.shifts;
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
        this.equipmentSelected = this.equipemntItems;

        this.subAreaItems1 = this.commServices.getSubAreas();
        this.subAareSelected1 = this.subAreaItems1[0];

        this.commServices.initDatePicker($('#dateTimeRange'), 'YYYY-MM-DD HH:mm:ss');
    }

    clearEquipmentSelected() {
        this.equipmentSelected = [];
    }
    equipmentSelectedOk() {
        this.dddb.instance.close();
    }

    onSubAreaSelectedChange(subArea: any): void {
        this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);
        this.equipmentSelected = this.equipemntItems;
    }
    /**
     * 获取表格数据
     */
    getReportData(): void {

        const queryModel: QueryModel = new QueryModel();
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        queryModel.queryResourceIds = this.equipmentSelected.map(e => e.equipment);
        queryModel.shift = this.shiftSelected.shiftId;
        this.commServices
            .getList(queryModel, ReportUrls.CTWpConfigListUrl)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.items = new Array();
                    model.data.detail.forEach(e => {
                        if (this.equipmentSelected.find(el => el.equipment === e.equipment && el.subArea === e.subArea) !== undefined) {
                            this.items.push(e);
                        }
                    });
                    this.equipmentSelected.forEach(e => {
                        if (this.items.find(el => el.equipment === e.equipment && el.subArea === e.subArea) === undefined) {
                            this.items.push({
                                subArea: e.subArea, equipment: e.equipment, ct: 0,
                                minValue: 0, maxValue: 0, sxTarget: 0, xxTarget: 0, isShow: 1
                            });
                        }
                    });

                    this.timeItems = model.data.timerange;

                } else {
                    alert(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    updateDetail() {
        this.commServices
            .getList(this.items, ReportUrls.CTWpConfigaddWPUrl)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    alert(model.resultMessage);
                } else {
                    alert(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    updateTimeRange() {
        this.commServices
            .getList(this.timeItems, ReportUrls.CTWpConfigupdateTimeUrl)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    alert(model.resultMessage);
                } else {
                    alert(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    saveTimeRange() {
        const start = this.getTime('dateTimeRange').split(' - ')[0];
        const end = this.getTime('dateTimeRange').split(' - ')[1];
        this.commServices
            .getList([{
                subArea: this.subAareSelected1.subArea,
                startTime: start, endTime: end,
                sxTarget: this.sxTarget,
                xxTarget: this.xxTarget
            }], ReportUrls.CTWpConfigaddWPTimeUrl)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    alert(model.resultMessage);
                } else {
                    alert(model.resultCode + ',' + model.resultMessage);
                }
            });

    }

    delTime() {
        if (confirm('是否删除')) {
            this.commServices.delete(this.timeItems, ReportUrls.CTWpConfigDeleteTimeUrl).subscribe(model => {
                if (model.resultCode === 200) {
                    this.getReportData();
                    alert(model.resultMessage);
                } else {
                    // console.log(model.resultCode + ',' + model.resultMessage);
                    //alert('下载失败！' + model.resultMessage);
                }
            });
        }
    }
    getTime(id: any): String {
        return $('#' + id).val();
    }
}
